/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.junit.Test;

/**
 * A place where I test out things i'm not sure about.
 * @author J Patrick Davenport
 *
 */
public class ScratchTests {
	@Test
	public void stringSpliting() {
		final String onePair = "name=value";
		final String[] splitQuery = onePair.split("&");
		assertNotNull(splitQuery);
		assertEquals(onePair, splitQuery[0]);
	}

	@Test
	public void speedTest() throws InterruptedException {
		// Check adding speed
		final List<Integer> list = new ArrayList<Integer>();
		final long arrayAdd = this.add(list);
		System.out.println("Adding ArrayList took: " + arrayAdd + " ms.");
		Thread.sleep(2000);

		final List<Integer> linkedList = new LinkedList<Integer>();
		final long linkedAdd = this.add(linkedList);
		System.out.println("Adding LinkedList took: " + linkedAdd + " ms.");

		System.out.println("Did ArrayList add quicker than LinkedList? " + (arrayAdd < linkedAdd));

		// Check reading
		final long arrayRead = this.read(list);
		final long linkedArrayRead = this.read(linkedList);
		System.out.println("Reading ArrayList took: " + arrayRead + " ms.");
		System.out.println("Reading LinkedList took: " + linkedArrayRead + " ms.");
		System.out.println("Did ArrayList read quicker than LinkedList? " + (arrayRead < linkedArrayRead));
	}

	private long read(final List<Integer> list) {
		final long start = System.currentTimeMillis();
		for (@SuppressWarnings("unused")
		final Integer i : list) {

		}
		final long end = System.currentTimeMillis();
		return (end - start);
	}

	private long add(final List<Integer> list) {
		final int size = 1000;

		final long start = System.currentTimeMillis();
		for (int i = 0; i < size; i++) {
			list.add(Integer.valueOf(i));
		}
		final long end = System.currentTimeMillis();
		return (end - start);
	}
}
